﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.ComponentModel;
using System.Collections.ObjectModel;
using carXapp2;
using carXapp2.classes;

namespace carXapp2.FuncPages
{
    public partial class CarStatsInfo : PhoneApplicationPage
    {
        private int carID;
        bool fVisibility= false;
        bool MVisibility = false;
        
        public CarStatsInfo()
        {
            InitializeComponent();
            //this.DataContext = App.ViewModel;

        }

        private double _mpg;
        public double MPG
        {
            get { return _mpg; }
            set
            {
                _mpg = value;
                NotifyPropertyChanged("MPG");

            }
        }
        private void LoadData(int id)
        {
            var fuelInfoWithCarID = from fuelInfo info in App.ViewModel.carDataDb.fuelInfo
                                    where info.CarID == id
                                    orderby info.Date ascending
                                    select info;
            var fuel = new ObservableCollection<fuelInfo>(fuelInfoWithCarID);

            var MaintInfoWithCarID = from maintInfo info in App.ViewModel.carDataDb.maintInfo
                                     where info.CarID == id
                                     orderby info.Date ascending
                                     select info;
            var maint = new ObservableCollection<maintInfo>(MaintInfoWithCarID);

            CarStats stats = new CarStats(maint,fuel);
            MPG = stats.GetOverallMPG();
            txtmpg.Text = MPG.ToString();
            int tMiles = stats.GetTotalMiles();
            float tCost = stats.GetTotalFuelCost();
            int tDays = stats.GetTotalDays();
            double MlPDy, FCoPDy, MlPMt,FCoPMo;
            try
            {
                MlPDy = tMiles / tDays;
                MlPDy = Math.Round(MlPDy, 2);
                MlPMt = (tMiles *30) / tDays;
                MlPMt = Math.Round(MlPMt, 2);
            }
            catch (DivideByZeroException)
            {
                 MlPDy = 0;
                 MlPMt = 0;
            }
            try
            {
                 FCoPDy = tCost / tDays;
                 FCoPDy = Math.Round(FCoPDy, 2);
                 FCoPMo = tCost * 30 / tDays;
                 FCoPMo = Math.Round(FCoPMo, 2);
           
            }
            catch (DivideByZeroException)
            {
                 FCoPDy = 0;
                 FCoPMo = 0;
            }

           
            txtMiles.Text = tMiles.ToString();
            txtFuelCost.Text = tCost.ToString();
            txtFCoPd.Text = FCoPDy.ToString();
            txtFCoPm.Text = FCoPMo.ToString();
            txtMiPd.Text = MlPDy.ToString();
            txtMiPm.Text = MlPMt.ToString();
            //section to calculate maint

            float tMaCo = stats.GetTotalMaintCost();
            float tMaLCo = stats.GetTotalLoborCost();
            float tMaPrCo = stats.GetTotalPartsCost();
            txtToMaCo.Text = tMaCo.ToString();
            txtMaLco.Text = tMaLCo.ToString();
            txtMaPco.Text = tMaPrCo.ToString();
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            string cid;
            NavigationContext.QueryString.TryGetValue("carID", out cid);
            int.TryParse(cid,out carID);
            grid1.DataContext = App.ViewModel.SingleCarRecord;
            grid2.DataContext = this;
            LoadData(carID);
        }
        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        // Used to notify Silverlight that a property has changed.
        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion

        private void fuelBtn_Click(object sender, RoutedEventArgs e)
        {
            fVisibility = !fVisibility;
            if (!fVisibility)
            {
                txtFCoPd.Visibility = System.Windows.Visibility.Collapsed;
                txtFCoPm.Visibility = System.Windows.Visibility.Collapsed;
                txtMiPd.Visibility = System.Windows.Visibility.Collapsed;
                txtMiPm.Visibility = System.Windows.Visibility.Collapsed;
                lblFcoPd.Visibility = System.Windows.Visibility.Collapsed;
                lblFcoPm.Visibility = System.Windows.Visibility.Collapsed;
                lblFmiPd.Visibility = System.Windows.Visibility.Collapsed;
                lblFMiPm.Visibility = System.Windows.Visibility.Collapsed;
            }
            else
            {
                txtFCoPd.Visibility = System.Windows.Visibility.Visible;
                txtFCoPm.Visibility = System.Windows.Visibility.Visible;
                txtMiPd.Visibility = System.Windows.Visibility.Visible;
                txtMiPm.Visibility = System.Windows.Visibility.Visible;
                lblFcoPd.Visibility = System.Windows.Visibility.Visible;
                lblFcoPm.Visibility = System.Windows.Visibility.Visible;
                lblFmiPd.Visibility = System.Windows.Visibility.Visible;
                lblFMiPm.Visibility = System.Windows.Visibility.Visible;
            }
        }

        private void btnMaint_Click(object sender, RoutedEventArgs e)
        {
            MVisibility = !MVisibility;
            if (!MVisibility)
            {
                txtMaLco.Visibility = System.Windows.Visibility.Collapsed;
                txtMaPco.Visibility = System.Windows.Visibility.Collapsed;
                txtToMaCo.Visibility = System.Windows.Visibility.Collapsed;
                lblMaLco.Visibility = System.Windows.Visibility.Collapsed;
                lblMaPco.Visibility = System.Windows.Visibility.Collapsed;
                lblMaToCo.Visibility = System.Windows.Visibility.Collapsed;
            }
            else
            {
                txtMaLco.Visibility = System.Windows.Visibility.Visible;
                txtMaPco.Visibility = System.Windows.Visibility.Visible;
                txtToMaCo.Visibility = System.Windows.Visibility.Visible;
                lblMaLco.Visibility = System.Windows.Visibility.Visible;
                lblMaPco.Visibility = System.Windows.Visibility.Visible;
                lblMaToCo.Visibility = System.Windows.Visibility.Visible;

 
            }
        }

    }
}